package com.boshao.classschedule.adapter;

import java.util.List;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;

import com.boshao.classschedule.R;
import com.boshao.classschedule.fragment.CalendarFragment.AppOnItemClickListener;
import com.boshao.classschedule.listener.CalendarFragmentCallBack;
import com.boshao.classschedule.model.DateInfo;
import com.boshao.classschedule.utils.DataUtils;
import com.boshao.classschedule.utils.TimeUtils;

/**
 * 日历左右滑动的PagerAdapter
 * @author boshao
 *
 */
public class CalendarViewPagerAdapter extends PagerAdapter{
	
	private CalendarGridAdapter mGridAdapter;
	private GridView mCurrentGridView;
	private GridView mGridView;
	private int mCurrentYear;
	private int mCurrentMonth;
	private int mCurrentDay;
	private int mSelectedDay;
	private List<DateInfo> mCurrentInfos = null; 
	public List<DateInfo> mDateInfos = null;
	private Context mContext;
	private AppOnItemClickListener mCalendarOnItemClickListener;

	public CalendarViewPagerAdapter(Context context,CalendarFragmentCallBack callBack) {
		mCurrentYear = callBack.getCurrentYear();
		mCurrentMonth = callBack.getCurrentMonth();
		mCurrentDay = callBack.getCurrentDay();
		mSelectedDay = callBack.getSelectedDay();
		mCalendarOnItemClickListener = callBack.getCalendarOnItemClickListener();
		mContext = context;
	}

	@Override
	public Object instantiateItem(ViewGroup container, int position) {
		GridView gv = initCalendarView(position);
		gv.setId(position);
		container.addView(gv);
		return gv;
	}
	
	@Override
	public int getCount() {
		return 1000;
	}

	@Override
	public boolean isViewFromObject(View view, Object object) {
		return view == object;
	}

	@Override
	public void destroyItem(ViewGroup container, int position, Object object) {
		container.removeView((View) object);
	}
	
	@Override
	public void setPrimaryItem(ViewGroup container, int position, Object object) {
		mCurrentGridView = (GridView) object;
	}
	
	public CalendarGridAdapter getGridAdapter() {
		return mGridAdapter;
	}

	public GridView getCurrentGridView() {
		return mCurrentGridView;
	}
	
	/**
	 * 初始化日历的gridview
	 * 
	 * */
    private GridView initCalendarView(int position) {
    	int year = TimeUtils.getTimeByPosition(position,mCurrentYear,mCurrentMonth, "year");
    	int month = TimeUtils.getTimeByPosition(position,mCurrentYear, mCurrentMonth, "month");
    	String formatDate = TimeUtils.getFormatDate(year, month);
    	try {
    		mDateInfos = TimeUtils.initCalendar(formatDate, month);
    	} catch (Exception e) {
    		e.printStackTrace();
    	}
    	mGridView = (GridView) LayoutInflater.from(mContext).inflate(R.layout.gridview_calendar,null);
    	mGridAdapter = new CalendarGridAdapter(mContext, mDateInfos);
    	if (position == 500) {
    		mCurrentInfos = mDateInfos;
    		int currentPos = DataUtils.getDayFlag(mDateInfos,mCurrentDay);
    		mGridAdapter.setCurrentPosition(currentPos);
    	}
    	mGridView.setAdapter(mGridAdapter);
    	mGridView.setOnItemClickListener(mCalendarOnItemClickListener);
    	return mGridView;
    }
	
}
